﻿#region MIT Licenze
//The MIT License

//Copyright (c) 2008 Sebastian Faltoni, dotnetfireball.org

//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:

//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.

//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace Fireball.OpenXML.Word
{
    public class StyleParagraphProperties : OpenXMLEntity
    {

        /// <summary>
        /// Keep All Lines on one page
        /// </summary>
        public bool KeepLines
        {
            get;
            set;
        }

        /// <summary>
        /// Keep this paragraph with next paragraph
        /// </summary>
        public bool KeepNext
        {
            get;
            set;
        }

        //adjustRightInd
        public bool AdjustRightIndent
        {
            get;
            set;
        }

        //cnfStyle BitMask
        public object ConditionalFormattingStyle
        {
            get;
            set;
        }

        //Can't be nested
        public ParagraphRevisionInformation Revision
        {
            get;
            set;
        }

        public StyleRunProperties RunProperties
        {
            get;
            set;
        }

        public ParagraphIndentation Indent
        {
            get;
            set;
        }

        public ParagraphBorder BorderLeft
        {
            get;
            set;
        }

        public ParagraphBorder BorderRight
        {
            get;
            set;
        }

        public ParagraphBorder BorderBottom
        {
            get;
            set;
        }

        public ParagraphBorder BorderTop
        {
            get;
            set;
        }

        public ParagraphSpacing Spacing
        {
            get;
            set;
        }

        public override System.Xml.Linq.XContainer ToXml()
        {
            XElement xParagraph = new XElement(NS.WM + "pPr");

            if (this.KeepNext)
                xParagraph.Add(new XElement(NS.WD + "keepNext"));

            if (this.KeepLines)
                xParagraph.Add(new XElement(NS.WD + "keepLines"));

            #region Borders
            XElement pBdr = new XElement(NS.WM + "pBdr");

            if (this.BorderLeft != null)
                pBdr.Add(this.BorderLeft.ToXml());

            if (this.BorderRight != null)
                pBdr.Add(this.BorderRight.ToXml());

            if (this.BorderTop != null)
                pBdr.Add(this.BorderTop.ToXml());

            if (this.BorderBottom != null)
                pBdr.Add(this.BorderBottom.ToXml());

            if (pBdr.Elements().Count() > 0)
                xParagraph.Add(pBdr);
            #endregion

            xParagraph.Add(new XElement(NS.WM + "adjustRightInd",
                new XAttribute(NS.WM + "val", this.AdjustRightIndent.ToOnOff())));

            //TODO:
            if (this.ConditionalFormattingStyle != null)
                xParagraph.Add(new XElement(NS.WM + "cnfStyle",
                    new XAttribute(NS.WM + "val", this.ConditionalFormattingStyle.ToString())));


            if (this.Indent != null)
            {
                xParagraph.Add(this.Indent.ToXml());
            }

            if (RunProperties != null)
            {
                xParagraph.Add(RunProperties.ToXml());
            }

            if (this.Revision != null)
            {
                xParagraph.Add(this.Revision.ToXml());
            }
            if (this.Spacing != null)
            {
                xParagraph.Add(this.Spacing.ToXml());
            }


            return xParagraph;
        }

        private void ParseInternal(XElement pPr)
        {
            var keepNext = pPr.Element(NS.WM + "keepNext");

            if (keepNext != null)
            {
                this.KeepNext = true;
            }

            var keepLines = pPr.Element(NS.WM + "keepLines");

            if (keepLines != null)
            {
                this.KeepLines = true;
            }

            var adjustRightInd = pPr.Element(NS.WM + "adjustRightInd");

            if (adjustRightInd != null)
            {
                this.AdjustRightIndent = true;
            }

            var cnfStyle = pPr.Element(NS.WM + "cnfStyle");

            if (cnfStyle != null)
            {
                this.ConditionalFormattingStyle = cnfStyle.Attribute(NS.WM + "val").Value;
            }

            var ind = pPr.Element(NS.WM + "ind");

            if (ind != null)
            {
                this.Indent = ParagraphIndentation.Parse(ind);
            }

            var pPrChange = pPr.Element(NS.WM + "pPrChange");

            if (pPrChange != null)
            {
                this.Revision = ParagraphRevisionInformation.Parse(pPrChange);
            }

            var pBdr = pPr.Element(NS.WM + "pBdr");

            if (pBdr != null)
            {
                var bLeft = pBdr.Element(NS.WM + "left");

                if (bLeft != null)
                {
                    this.BorderLeft = ParagraphBorder.Parse(bLeft);
                }
                var bRight = pBdr.Element(NS.WM + "right");

                if (bRight != null)
                {
                    this.BorderRight = ParagraphBorder.Parse(bRight);
                }

                var bTop = pBdr.Element(NS.WM + "top");

                if (bTop != null)
                {
                    this.BorderTop = ParagraphBorder.Parse(bTop);
                }

                var bBottom = pBdr.Element(NS.WM + "bottom");

                if (bBottom != null)
                {
                    this.BorderBottom = ParagraphBorder.Parse(bBottom);
                }
            }

            var spacing = pPr.Element(NS.WM + "spacing");

            if (spacing != null)
            {
                this.Spacing = ParagraphSpacing.Parse(spacing);
            }


            var rPr = pPr.Element(NS.WM + "rPr");

            if (rPr != null)
            {
                this.RunProperties = StyleRunProperties.Parse(rPr);
            }
        }

        public static StyleParagraphProperties Parse(XElement pPr)
        {
            StyleParagraphProperties p = new StyleParagraphProperties();

            p.ParseInternal(pPr);

            return p;
        }
    }
}
